package com.xianbeiying.app.ckgl.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.GhPersonBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.util.PhoneUtil;
import com.xianbeiying.app.ckgl.util.ToastUtil;

/***
 * 温室主人
 * */
public class GhPersonActivity extends BaseActivity  implements View.OnClickListener, HttpCallBackUtil {
    private ImageView iv_title_left;
    private TextView tv_title;
    private Button gh_btn;
    private EditText et_phone,et_name;
    private SetModel setModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gh_person);
        initView();
    }

    private void initView() {
        MainActivity.fragmentid=3;
        setModel=new SetModel(this);
        setModel.addResponseListener(this);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        gh_btn = (Button) findViewById(R.id.gh_btn);
        tv_title.setText("仓库主人");
        iv_title_left.setOnClickListener(this);
        gh_btn.setOnClickListener(this);
        if(MyApplication.loginBean!=null){
            et_phone.setText(MyApplication.loginBean.getTEL());
            et_name.setText(MyApplication.loginBean.getREALNAME());
        }
    }

    @Override
    public void onCallBackData(String url, Object content) {
            if(url.equals("温室主人")){
                if(content!=null){
                    GhPersonBean ghPersonBean= JSON.parseObject(new String((byte[])content),GhPersonBean.class);
                        if(ghPersonBean.isR()){
                            ToastUtil.showInfo(GhPersonActivity.this,"修改成功");
                            MyApplication.loginBean.setREALNAME(userNames);
                            MyApplication.loginBean.setTEL(phones);
                            finish();
                        }else{
                            ToastUtil.showInfo(GhPersonActivity.this,"修改失败");
                        }
                }
            }
    }
    String userNames;
    String phones;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.gh_btn:
                 userNames = et_name.getText().toString().trim();
                 phones= et_phone.getText().toString().trim();
                if(userNames.isEmpty()){
                    ToastUtil.showInfo(this,"姓名不能为空");
                    return;
                }
                if(phones.isEmpty()){
                    ToastUtil.showInfo(this,"手机号不能为空");
                    return;
                }
                if (!PhoneUtil.isPhone(phones)){
                    ToastUtil.showInfo(this,"手机号格式不正确");
                    return;
                }
                setModel.SETGHPerson(userNames,phones,MyApplication.loginBean.getUSERID(),true);
                break;
        }
    }
}
